package org.cuit.lmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.core.BaseForecastData;
import org.cuit.lmq.core.service.ForecastServiceImpl;
import org.cuit.lmq.domain.common.BaseContextHolder;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.domain.po.BodyInfo;
import org.cuit.lmq.domain.po.TrainInfo;
import org.cuit.lmq.service.BodyInfoService;
import org.cuit.lmq.service.TrainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Seon
 * @date 2023/3/12
 */
@Slf4j
@RestController
@RequestMapping("/analyse")
public class ForecastController {
    @Autowired
    private ForecastServiceImpl forecastService;

    @Autowired
    private BodyInfoService bodyInfoService;

    @Autowired
    private TrainInfoService trainInfoService;

    //so_weak,eat_more,beautiful_handsome,fat,need_lose_weight
    @GetMapping("/advise")
    public Result<?> executeForecast(@RequestParam("date") String dateSource) {
        long userId = BaseContextHolder.getCurrentUserId();

        LambdaQueryWrapper<BodyInfo> bodyWrapper = new LambdaQueryWrapper<>();
        bodyWrapper.eq(BodyInfo::getUserId, userId);
        bodyWrapper.orderByDesc(BodyInfo::getCreateTime);
        BodyInfo bodyInfo = bodyInfoService.list(bodyWrapper).get(0);

        /*
        开始对训练数据进行封装，更新后的策略为：
        提取出date当天的所有训练数据，对其进行平均取值
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateSource);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        long multiply = 1000 * 60 * 60 * 24;
        long multiDate = date.getTime() + multiply;
        Date addDate = new Date(multiDate);
        LambdaQueryWrapper<TrainInfo> trainWrapper = new LambdaQueryWrapper<>();
        trainWrapper.eq(TrainInfo::getUserId, userId);
        trainWrapper.between(TrainInfo::getDate, date, addDate);
        List<TrainInfo> trainInfos = trainInfoService.list(trainWrapper);

        TrainInfo trainInfo = TrainInfo.builder()
                .avgHeartRate(trainInfos.parallelStream().mapToDouble(TrainInfo::getAvgHeartRate).average().orElse(-1D))
                .trainTime(trainInfos.parallelStream().mapToDouble(TrainInfo::getTrainTime).average().orElse(-1D))
                .calorieBrun(trainInfos.parallelStream().mapToDouble(TrainInfo::getCalorieBrun).average().orElse(-1D))
                .build();


        String weight = bodyInfo.getWeight()>70? (bodyInfo.getWeight()>100? "high": "normal"): "low";
        String bmi = bodyInfo.getBmi()>18.5? (bodyInfo.getBmi()>23.9? "high": "normal"): "low";
        String bodyStatus = bodyInfo.getBodyStatus().getValue();

        String trainTime, avgHeartRate, calorieBrun;
        if (trainInfo.getCalorieBrun() > 1000) {
            calorieBrun = "devil";
        } else if (trainInfo.getCalorieBrun() > 800) {
            calorieBrun = "high";
        } else if (trainInfo.getCalorieBrun() > 600) {
            calorieBrun = "normal";
        } else {
            calorieBrun = "low";
        }

        if (trainInfo.getAvgHeartRate() > 192) {
            avgHeartRate = "devil";
        } else if (trainInfo.getAvgHeartRate() > 150) {
            avgHeartRate = "high";
        } else if (trainInfo.getAvgHeartRate() > 100) {
            avgHeartRate = "normal";
        } else {
            avgHeartRate = "low";
        }

        if (trainInfo.getTrainTime() > 192) {
            trainTime = "longlong";
        } else if (trainInfo.getTrainTime() > 150) {
            trainTime = "long";
        } else if (trainInfo.getTrainTime() > 100) {
            trainTime = "normal";
        } else {
            trainTime = "short";
        }

        BaseForecastData forecastData = BaseForecastData.builder()
                .weight(weight)
                .bmi(bmi)
                .bodyStatus(bodyStatus)
                .trainTime(trainTime)
                .avgHeartRate(avgHeartRate)
                .calorieBrun(calorieBrun)
                .result("wait forecast")
                .build();

        log.info("{}号用户的预测数据为{}", userId, forecastData);
        String suggestion = forecastService.getSuggestion(forecastData);

        return Result.success(suggestion);
    }
}
